$(document).ready(function(){
	initData(1);
	
	/*JQuery 限制文本框只能输入数字 */
  $("#goPage").keyup(function (){
        $(this).val($(this).val().replace( /\D|^0/g, ''));
   }).bind("paste", function(){ //CTR+V事件处理
        $(this).val($(this).val().replace( /\D|^0/g, ''));
   }).css("ime-mode", "disabled"); //CSS设置输入法不可用
  
	$("#ok").click(function(){
//	  alert();
	  var rollout=trim($("#rolloutvalue").val());
	if(!isNaN(rollout)&&rollout!="")
	{
		var courseid=$("#courseid").val();
		var stuid=$("#stuid").val();
		var terid=$("#terid").val();
		var deptid=$("#deptid").val();
		var rollout=$("#rolloutvalue").val();
		var path = $("#path").val()+"/UpdateRollOut.action?courseid="+courseid+"&terid="+terid+"&stuid="+stuid+"&rollout="+rollout+"&deptid="+deptid;
		$.ajax({
			type:"post",
			async:false,
			url:path,
			success:function(data){
			if(data=="true"){
				$("#rolloutvalue").val("");
	 			 $("#updaterollout").hide();
	 			 //window.location.reload();
	 			 initData(1);
				alert('更新成功');
			}
			else
			{
				alert('更新失败');
			}
			}});
		  
	}
	else
	{
		$("#rolloutvalue").val("");
		$("#rolloutvalue").focus();
		alert('请输入合法数字！');
		
	}
  });
  $("#close").click(function(){
	  $("#rolloutvalue").val("");
	  $("#updaterollout").hide();
  });
});


function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}


//列表显示
function initData(currentPage){
	 $("#updaterollout").hide();
	var terId = $("#terID").val();
	var schId = $("#schID").val();
	var subId = $("#subID").val();
	var graId = $("#graID").val();
	//var feeClass=$("#feeClass").val();
	var surplusfee=$("#surplusfee").val();
//	var teaName = $("#teaName").val();
	var stuName = $("#stuName").val();
	var startTime = $("#startTime").val();
	var endTime = $("#endTime").val();
	

	
	var path = $("#path").val()+"/PaginationSecond.action";
	var pageSize = 25;
	var pageCount = "";
	
//	if(claId == 0 && trim(stuname).length <= 0){
//		$("#divshow").show();$("#divload,#page").hide();
//		$("#myTable").append("<tr><td colspan='12'>没有匹配的数据！</td></tr>");
//	}else{
	   	$.ajax({
		      type: "post",
		      url: path,
		      dataType: "json",
		      data: "currentPage="+currentPage
	          +"&pageSize="+pageSize
	          +"&terId="+terId
	          +"&schId="+schId
	          +"&subId="+subId
	          +"&graId="+graId
	          +"&endTime="+endTime
	          +"&startTime="+startTime
	         // +"&feeClass="+feeClass
	          +"&surplusfee="+surplusfee
//	          +"&teaName="+encodeURI(encodeURI(trim(teaName)))
	          +"&stuName="+encodeURI(encodeURI(trim(stuName))),
	          
		      cache: true, //默认为true,设为false将不缓存页面，开发时使用。正式使用时可注释掉。
		      beforeSend: function(){$("#divload").show();$("#divshow,#page").hide();},
		      complete: function(){$("#divshow,#page").show();$("#divload").hide();},
		      success: function(json){
		    	  $("#myTable tr:gt(0)").remove();//移除Id为result的表格里的行，从第二行开始
		    	  //总记录数
		    	  $("#stuName").val(stuName);
		    	  pageCount = json[0].pageCount;
		    	  if(pageCount != 0){
	    			 for(var i = 0; i < json.length-1; i++){
	    				 
		    			 $("#dataShow").append("<tr>"+
		    			  "<td>"+((currentPage-1)*pageSize+i+1)+"</td>"+
				          "<td>"+json[i].stuName+"</td>"+
				          "<td>"+json[i].terName+"</td>"+
				          "<td>"+json[i].schName+"</td>"+
				          "<td>"+json[i].subName+"</td>"+
				           "<td>"+json[i].courseName+"</td>"+
				          "<td>"+json[i].graName+"</td>"+
//				          "<td>"+json[i].claName+"</td>"+
//				          "<td>"+json[i].teaName+"</td>"+
				          "<td>"+json[i].shouldFee+"</td>"+
				          "<td>"+json[i].actualFee+"</td>"+
				          "<td>"+json[i].carryOver+"</td>"+
				          "<td>"+json[i].otherCheaper+"</td>"+
				           "<td>"+json[i].booksum+"</td>"+//书本费
				         // "<td>"+json[i].unPayFee+"</td>"+//未缴清学费
				          "<td>"+json[i].refundFee+"</td>"+//退费金额
				           "<td id='roll"+i+"'>"+json[i].rollout+"</td>"+//转出费用
//				           "<td></td>"+//未缴清学费
				          "<td>"+json[i].claNum+"</td>"+
				          "<td>"+json[i].yiNum+"</td>"+
				        //  "<td>"+json[i].syNum+"</td>"+
				          "<td>"+json[i].discount+"</td>"+
				     	"<td>"+json[i].feeperClass+"</td>"+//单次课费用
				     	"<td>"+json[i].surplusbefore+"</td>"+//往期结余
				     	"<td>"+json[i].alreadyFee+"</td>"+//消耗费用
				          "<td>"+json[i].surplusFee+"</td>"+//剩余费用
				          "<td id='updatetd"+i+"'><a href='javascript:void(0);' onclick='updaterollout("+terId+","+json[i].stuId+","+json[i].courseid+","+json[i].deptid+","+i+");'>修改</a></td>"+//剩余费用
//				         "<td>"+json[i].recordStaff+"</td>"+
				        "</tr>");
	    			 }
	    			  $("#dataShow").append("<tr>"+
	    					 "<td colspan='7'><font color='red'>汇 总</font></td>"+
	    					 "<td>"+json[json.length-1].should+"元</td>"+
	    					 "<td>"+json[json.length-1].actual+"元</td>"+
	    					  "<td>"+json[json.length-1].sumCarryOver+"元</td>"+
	    					 
	    					 "<td>"+json[json.length-1].sumotherCheaper+"元</td>"+
	    					  "<td>"+json[json.length-1].sumbooksum+"元</td>"+
	    					// "<td>--</td>"+
	    					 "<td>"+json[json.length-1].refundsum+"</td>"+
	    					 "<td>"+json[json.length-1].sumRollOut+"</td>"+
	    					 "<td>"+json[json.length-1].cln+"</td>"+
	    					 "<td>"+json[json.length-1].sumYiNum+"</td>"+
	    					// "<td>--</td>"+
	    					 "<td>--</td>"+
	    					 "<td>--</td>"+
	    					 "<td>"+json[json.length-1].surplusFeeBefore+"</td>"+
	    					 "<td>"+json[json.length-1].sumalreadyfee+"</td>"+
	    					 "<td>"+json[json.length-1].sumsurplusFee+"</td>"+
	    					 "<td>--</td>"+
	    			 "</tr>");
	    			//汇 总	    			 
//	    			 $("#dataShow").append("<tr>"+
//	    					 "<td colspan='8'><font color='red'>汇 总</font></td>"+
//	    					 "<td>"+json[0].should+"元</td>"+
//	    					 "<td>"+json[0].actual+"元</td>"+
//	    					 "<td>--</td>"+
//	    					 "<td>"+json[0].cln+"</td>"+
//	    					 "<td>"+json[0].sumYiNum+"</td>"+
//	    					 "<td>"+json[0].synum+"</td>"+
//	    					 "<td>--</td>"+
//	    					 "<td>--</td>"+
//	    					 "<td>--</td>"+
//	    					 "<td>--</td>"+
//	    					 "<td>--</td>"+
//	    			 "</tr>");
	      		 }else{
	      			 $("#dataShow").append("<tr><td colspan='21'>没有匹配的数据！</td></tr>");
		    	 }
			    	  
		    	  //行交替颜色
		    	  $("#myTable tr:even").css("background-color","#f5f5f5");
			  		$("#myTable tr").live("mouseenter",function(){
			  			$(this).addClass("lightbluebg");
			  		}).live("mouseleave",function(){
			  			$(this).removeClass("lightbluebg");
			  	  });
	    	  
		    	  //漂亮的提示 哈哈
//		    	  $('.skyblue').poshytip({
//		    			className: 'tip-skyblue',
//		    			bgImageFrameSize: 9,
//		    			offsetX: 0,
//		    			offsetY: 20
//		    	  });
			    	  
		    	   //设定分页
		    	    $("#totalRows").html(pageCount);//总记录数
		    	    $("#currentPage").html(currentPage);//当前页数
		    	    $("#pageSize").html(pageSize); //每页显示记录数
		    	    
		    	    var totalPageNum = 0;
		    	    if((pageCount % pageSize) == 0){
			    	     $("#totalPages").html(pageCount / pageSize);
			    	     totalPageNum = pageCount / pageSize;
		    	    }else{
			    	     $("#totalPages").html(parseInt(pageCount / pageSize) + 1);
			    	     totalPageNum = parseInt(pageCount / pageSize) + 1;
		    	    }
		    	    $("#totalPge").val(totalPageNum); //将总页数赋值给totalPge隐藏域
		    	    
		    	    //设定上一页，下一页  首页、尾页的链接
		    	    if(pageCount <= pageSize){
		    	    	 $("#first").attr("href","javascript:void(0);");
		    	    	 $("#previous").attr("href","javascript:void(0);");
		    	    	 $("#next").attr("href","javascript:void(0);");
		    	    	 $("#last").attr("href","javascript:void(0);");
		    	    	 $("#go").removeAttr("onclick");
		    	    }else if(currentPage == 1){
		    	    	 $("#first").attr("href","javascript:void(0);");
		    	    	 $("#previous").attr("href","javascript:void(0);");
		    	    	 $("#next").attr("href","javascript:initData("+(currentPage+1)+");");
		    	    	 $("#last").attr("href","javascript:initData("+totalPageNum+");");
		    	    }else if(currentPage == totalPageNum){
		    	    	 $("#first").attr("href","javascript:initData(1);");
		    	    	 $("#previous").attr("href","javascript:initData("+(currentPage-1)+");");
		    	    	 $("#next").attr("href","javascript:void(0);");
		    	    	 $("#last").attr("href","javascript:void(0);");
		    	    }else{
		    	    	 $("#first").attr("href","javascript:initData(1);");
		    	    	 $("#previous").attr("href","javascript:initData("+(currentPage-1)+");");
		    	    	 $("#next").attr("href","javascript:initData("+(currentPage+1)+");");
		    	    	 $("#last").attr("href","javascript:initData("+totalPageNum+");");
		    	    }
		      }//success END
		}); //ajax  END
//	}
}//列表结束


function updaterollout(terid,stuid,courseid,deptid,left1)
{
//	alert(left);	
	
	$("#courseid").val(courseid);
	$("#stuid").val(stuid);
	$("#terid").val(terid);
	$("#deptid").val(deptid);
	$("#updaterollout").css("height",100);
	$("#updaterollout").css("top",$("#updatetd"+left1+"").position().top);
	$("#updaterollout").css("left",$("#updatetd"+left1+"").position().left+50);
	$("#updaterollout").show();
	$("#rolloutvalue").val($("#roll"+left1+"").text());
	$("#rolloutvalue").focus();
}
//输入页数跳转页面
function goOnclick(){
	var pageNum = $("#goPage").val();
	var totalPge = $("#totalPge").val();
	if(pageNum != "" && parseInt(pageNum) <= parseInt(totalPge)){
		initData(pageNum);
	}else{
		$.dialog.tips('请输入总页数以内的整数！',3,'alert.gif');
	}
}
function openBackFeeWin(stuName,schName){
	var path = 'url:win/win.jsp?terId='+terId+'&stuId='+stuId+'&claId='+claId;
    $.dialog({ 
    	lock: true,
    	fixed: true,
    	title: '考勤详细',
    	width: '800px',
		height: 450,
    	content: path,
		cancelVal: '关闭',
		cancel: true
    });
}
function openWinOtherCheaper(terId,stuId,type){
	//var path = 'url:win/win.jsp?terId='+terId+'&stuId='+stuId+'&claId='+claId;
	var path = 'url:win/winOtherCheaper.jsp?terId='+terId+'&stuId='+stuId+'&subClassType='+type;
    $.dialog({ 
    	lock: true,
    	fixed: true,
    	title: '其他优惠',
    	width: '800px',
		height: 450,
    	content: path,
		cancelVal: '关闭',
		cancel: true
    });
}
function openWinSurplusBalance(terId,stuId,type){
	//var path = 'url:win/win.jsp?terId='+terId+'&stuId='+stuId+'&claId='+claId;
	var path = 'url:win/winSurplusBalance.jsp?terId='+terId+'&stuId='+stuId+'&subClassType='+type;
    $.dialog({ 
    	lock: true,
    	fixed: true,
    	title: '往期结余',
    	width: '800px',
		height: 450,
    	content: path,
		cancelVal: '关闭',
		cancel: true
    });
}
function openWin(terId,stuId,subName,courseid){
	//var path = 'url:win/win.jsp?terId='+terId+'&stuId='+stuId+'&claId='+claId;
	var path = 'url:win/winAlreadyClass.jsp?terId='+terId+'&stuId='+stuId+'&subName='+subName+'&courseid='+courseid;
    $.dialog({ 
    	lock: true,
    	fixed: true,
    	title: '考勤详细',
    	width: '800px',
		height: 450,
    	content: path,
		cancelVal: '关闭',
		cancel: true
    });
}

/**
 * 消耗费用明细 jinmingming
 * @param {Object} terId
 * @param {Object} stuId
 * @param {Object} type
 */
function openWinAlreadyFee(terId,stuId,type,GradeId,courseid){
	var path = 'url:win/winAlreadyFee.jsp?terId='+terId+'&stuId='+stuId+'&subClassType='+type+'&graId='+GradeId+'&courseid='+courseid;
    $.dialog({ 
    	lock: true,
    	fixed: true,
    	title: '消耗费用',
    	width: '800px',
		height: 450,
    	content: path,
		cancelVal: '关闭',
		cancel: true
    });
}

/**
 * 实收学费 jinmingming
 * @param {Object} studentId
 * @param {Object} SubjectCode
 * @param {Object} GradeCode
 * @param {Object} PeriodId
 */
function openWinActualFee(studentId,SubjectId,GradeId,PeriodId,courseid){
	var path = 'url:win/winActualFee.jsp?terId='+PeriodId+'&stuId='+studentId+'&graId='+GradeId+'&subId='+SubjectId+'&courseid='+courseid;
    $.dialog({ 
    	lock: true,
    	fixed: true,
    	title: '实收学费',
    	width: '800px',
		height: 450,
    	content: path,
		cancelVal: '关闭',
		cancel: true
    });
}